package com.huike.review.service.impl;

import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.mapper.MybatisReviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * mybatis复习使用的业务层
 * 注意该业务层和我们系统的业务无关，主要是让同学们快速熟悉系统的
 */
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private MybatisReviewMapper reviewMapper;

    /**
     * 新增 Review
     *
     * @param review
     */
    @Override
    public void addPostMapping(Review review) {
        review.setCreateBy(SecurityUtils.getUsername());
        review.setUpdateBy(SecurityUtils.getUsername());
        Date now = DateUtils.getNowDate();
        review.setCreateTime(now);
        review.setUpdateTime(now);
        reviewMapper.insertReview(review);
    }

    /**
     * 根据 id删除
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        reviewMapper.deleteById(id);
    }

    /**
     * 根据 id修改
     *
     * @param review
     */
    @Override
    public void updateById(Review review) {
        review.setUpdateBy("1");
        Date now = DateUtils.getNowDate();
        review.setUpdateTime(now);
        reviewMapper.updateById(review);
    }

    /**
     * 根据 id查询
     *
     * @param id
     * @return
     */
    @Override
    public Review getById(Long id) {
        return reviewMapper.getById(id);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public TableDataInfo getDataByPage(Integer pageNum, Integer pageSize) {
        Integer start = (pageNum - 1) * pageSize;
        List<Review> reviewList = reviewMapper.getDataByPage(start, pageSize);
        int total = reviewMapper.getTotalCount();
        return new TableDataInfo(reviewList, total);
    }

    @Override
    public List<Review> selectAll(Review review) {
        return reviewMapper.selectAll(review);
    }

}
